package com.zenith.storage.controller;

import com.zenith.common.annotation.request.SecurePostMapping;
import com.zenith.common.result.PageResult;
import com.zenith.common.result.Result;
import com.zenith.storage.po.dto.FileQueryDTO;
import com.zenith.storage.po.dto.UploadInitDTO;
import com.zenith.storage.service.StorageFileService;
import com.zenith.storage.po.vo.FileVO;
import com.zenith.storage.po.vo.UploadInitVO;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import jakarta.servlet.http.HttpServletResponse;
import jakarta.validation.Valid;

/**
 * 文件管理控制器
 */
@Slf4j
@RestController
@RequestMapping("/api/storage/file")
@RequiredArgsConstructor
public class StorageFileController {
    
    private final StorageFileService storageFileService;
    
    /**
     * 初始化上传
     */
    @SecurePostMapping("/initUpload")
    public Result<UploadInitVO> initUpload(@Valid @RequestBody UploadInitDTO uploadInitDTO) {
        try {
            UploadInitVO result = storageFileService.initUpload(uploadInitDTO);
            return Result.success(result);
        } catch (Exception e) {
            log.error("初始化上传失败", e);
            return Result.fail("初始化上传失败");
        }
    }
    
    /**
     * 上传文件
     */
    @SecurePostMapping("/uploadFile")
    public Result<FileVO> uploadFile(@RequestParam("file") MultipartFile file,
                                     @RequestParam("fileId") Long fileId) {
        try {
            UploadInitVO uploadInitVO = new UploadInitVO();
            uploadInitVO.setFileId(fileId);
            FileVO result = storageFileService.uploadFile(file, uploadInitVO);
            return Result.success(result);
        } catch (Exception e) {
            log.error("上传文件失败", e);
            return Result.fail("上传文件失败");
        }
    }
    
    /**
     * 下载文件
     */
    @SecurePostMapping("/downloadFile/{fileId}")
    public void downloadFile(@PathVariable Long fileId, HttpServletResponse response) {
        try {
            storageFileService.downloadFile(fileId, response);
        } catch (Exception e) {
            log.error("下载文件失败，fileId: {}", fileId, e);
            // 设置错误响应
            response.setStatus(HttpServletResponse.SC_INTERNAL_SERVER_ERROR);
            try {
                response.getWriter().write("文件下载失败");
            } catch (Exception ex) {
                log.error("写入错误响应失败", ex);
            }
        }
    }
    
    /**
     * 删除文件
     */
    @SecurePostMapping("/deleteFile/{fileId}")
    public Result<Boolean> deleteFile(@PathVariable Long fileId) {
        try {
            Boolean result = storageFileService.deleteFile(fileId);
            return Result.success(result);
        } catch (Exception e) {
            log.error("删除文件失败", e);
            return Result.fail("删除文件失败");
        }
    }
    
    /**
     * 获取文件详情
     */
    @SecurePostMapping("/getFileDetail/{fileId}")
    public Result<FileVO> getFileDetail(@PathVariable Long fileId) {
        try {
            FileVO result = storageFileService.getFileDetail(fileId);
            return Result.success(result);
        } catch (Exception e) {
            log.error("获取文件详情失败", e);
            return Result.fail("获取文件详情失败");
        }
    }
    
    /**
     * 分页查询文件列表
     */
    @SecurePostMapping("/listFiles")
    public Result<PageResult<FileVO>> listFiles(FileQueryDTO queryDTO) {
        try {
            PageResult<FileVO> result = storageFileService.listFiles(queryDTO);
            return Result.success(result);
        } catch (Exception e) {
            log.error("查询文件列表失败", e);
            return Result.fail("查询文件列表失败");
        }
    }
}